package org.example.software.controller;

import org.example.software.service.ToolsService;
import org.example.software.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.software.entity.Tools;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/tools")
public class ToolsController {

    @Autowired
    private ToolsService toolsService;

    // 添加工具
    @PostMapping("/add")
    public String addTools(@RequestHeader("Authorization") String token, @RequestBody Tools tools) {
        if (!JwtUtil.isAdmin(token)) {
            return "权限不足";
        }
        toolsService.addTools(tools);
        return "工具添加成功";
    }

    // 删除工具
    @DeleteMapping("/delete/{id}")
    public String deleteToolsById(@RequestHeader("Authorization") String token, @PathVariable int id) {
        if (!JwtUtil.isAdmin(token)) {
            return "权限不足";
        }
        toolsService.deleteToolsById(id);
        return "工具删除成功";
    }

    // 修改工具
    @PutMapping("/update")
    public String updateTools(@RequestHeader("Authorization") String token, @RequestBody Tools tools) {
        if (!JwtUtil.isAdmin(token)) {
            return "权限不足";
        }
        toolsService.updateTools(tools);
        return "工具信息更新成功";
    }

    // 查找所有工具
    @GetMapping("/all")
    public List<Tools> getAllTools() {
        return toolsService.getAllTools();
    }

    // 根据工具ID查找
    @GetMapping("/{id}")
    public List<Tools> getToolsById(@PathVariable int id) {
        return toolsService.getToolsById(id);
    }

    // 根据制造商查找
    @GetMapping("/manufacturerName/{manufacturerName}")
    public List<Tools> getToolsByName(@PathVariable String manufacturerName) {
        return toolsService.getToolsByName(manufacturerName);
    }

    // 根据工具名称查找
    @GetMapping("/toolName/{toolName}")
    public List<Tools> getToolsByToolName(@PathVariable String toolName) {
        return toolsService.getToolsByToolName(toolName);
    }


}
